package com.jiuli_travel.attraction_service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiuli_travel.attraction_service.mapper.AttractionMapper;
import com.jiuli_travel.attraction_service.model.cache.attraction.AttractionCollection;
import com.jiuli_travel.attraction_service.model.cache.attraction.AttractionLiked;
import com.jiuli_travel.attraction_service.model.dto.attraction.AttractionInsertRequest;
import com.jiuli_travel.attraction_service.model.dto.attraction.AttractionQueryRequest;
import com.jiuli_travel.attraction_service.model.dto.attraction_comment.AttractionCommentQueryRequest;
import com.jiuli_travel.attraction_service.model.entity.Attraction;
import com.jiuli_travel.attraction_service.model.vo.AttractionVO;
import com.jiuli_travel.attraction_service.service.IAttractionCommentService;
import com.jiuli_travel.attraction_service.service.IAttractionService;
import com.jiuli_travel.attraction_service.service.IEntertainmentProjectService;
import com.jiuli_travel.blog_service.model.entity.Team;
import com.jiuli_travel.user_service.model.vo.UserVO;
import com.jiuli_travel.user_service.service.IUserService;
import com.jiuli_travel.utils.MinDistanceUtil;
import com.jiuli_travel.utils.PageDTO;
import com.jiuli_travel.utils.UserHolder;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 景点表 服务实现类
 * </p>
 *
 * @author jiuli
 * @since 2024-04-01
 */
@Service
public class AttractionServiceImpl extends ServiceImpl<AttractionMapper, Attraction> implements IAttractionService {

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private AttractionLiked attractionLiked;

    @Autowired
    private AttractionCollection attractionCollection;

    @Autowired
    private IUserService userService;

    @Autowired
    private IAttractionCommentService attractionCommentsService;

    @Autowired
    private IEntertainmentProjectService entertainmentProjectService;


    //文本搜索景区内容（用es比较好一点，分词器嘛）
    @Override
    public List<AttractionVO> searchAttraction(String searchText) {
        // 1. 构造查询条件并查询
        List<Attraction> attractionList = lambdaQuery()
                .like(Attraction::getName, searchText)
                .list();
        
        // 2. 查询策略, 编辑距离算法 TODO 后面引入分词器，elasticsearch 才可适配
        Map<Attraction, Integer> attractionMap = new HashMap<>();
        attractionList.
                forEach(attraction -> {
                    int score = MinDistanceUtil.minDistance(attraction.getName(), searchText);
                    attractionMap.put(attraction, score);
                });

        // 3. 根据分数进行排序
        attractionList = attractionMap.entrySet()
                .stream()
                .sorted(Map.Entry.comparingByValue())
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());

        // 4. 转化格式
        List<AttractionVO> attractionVOS = BeanUtil.copyToList(attractionList, AttractionVO.class);

        // 5. 封装其它的数据
        attractionVOS
                .forEach(attractionVO -> {
                    // 5.1 评论区信息
                });

        return attractionVOS;
    }

    //发布个人 attraction
    @Override
    public void publishAttraction(AttractionInsertRequest attractionInsertRequest) {
        //图片信息不能为空
//        if(attractionInsertRequest.getImages() == null){
//            return;
//        }

        //获取发布该景区的用户信息
        UserVO user = UserHolder.getUser();

        /**
         * 1. 对于 attraction表 进行处理
         */
        Attraction attraction = BeanUtil.copyProperties(attractionInsertRequest, Attraction.class);
        attraction.setId(user.getId());
        //进行发布
        save(attraction);

        /**
         * TODO 对其他的附属内容进行处理
         */

    }

    // 删除个人 attraction
    @Override
    public void batchDelAttraction(List<Long> attractionIds) {
        /**
         * 对 attraction 表进行处理
         */
        //构造删除条件
        LambdaQueryChainWrapper<Attraction> delWrapper = lambdaQuery()
                .in(!attractionIds.isEmpty(), Attraction::getId, attractionIds);
        boolean remove = remove(delWrapper);
        if(!remove) return;

        /**
         * TODO 对和 attraction 有依赖关系的东西进行清理
         */

        attractionIds.forEach(attractionId -> {
            /**
             * TODO 与该景区有关的评论信息进行清空 （也可以进行后续异步处理）
             */
        });

    }
}
